<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button icon="el-icon-refresh" type="primary" size="mini" @click="Refresh"></el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="1"></el-option>
              <el-option label="手机号" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="realname" label="昵称"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="mobile" label="手机号码"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="130" align="center" prop="title" label="所属组别"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="130" align="center" prop="email" label="Email"></el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="130" align="center" prop="fUsername" label="推荐人"></el-table-column>
      <el-table-column v-if="colData[6].istrue" min-width="130" align="center" prop="name" label="VIP等级"></el-table-column>
      <el-table-column v-if="colData[7].istrue" min-width="130" align="center" prop="examine" label="状态">
        <template slot-scope="scope">
          <div class="state" :class="scope.row.examine == '1' ? 'on_line' : 'off_line'">
            <i class="iconfont icon-dian1"></i>
            <span>{{scope.row.examine == '1' ? '已审核' : '未审核'}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" min-width="140" align="center" prop="create_time" label="注册时间">
        <template slot-scope="scope">{{ scope.row.create_time | newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdEditExamine(scope.row)">审核</el-button>
            <el-button size="mini" @click="getdeleteExamine(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryExamine, queryIdEditExamine, deleteExamine } from "@/api/infos.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["用户名", "昵称", "手机号码", "所属组别", "Email", "推荐人", "VIP等级", "状态", "注册时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      listQuery: {
        startSize: 1,
        pageSize: 10,
        userId: localStorage.getItem("ID"),
        type: 1,
        username: undefined,
        mobile: undefined,
      },
      //删除弹窗
      deleteDialogs: {
        show: false,
        deleteText: '',
        deleteId: 0
      }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryExamine()
  },
  methods: {
    //删除
    getdeleteExamine(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = {
        userId: localStorage.getItem("ID"),
        last_ip: sessionStorage.getItem("Ip"),
        id: this.deleteDialogs.deleteId
      }
      deleteExamine(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryExamine()
      })
    },
    //根据ID审核用户
    getqueryIdEditExamine(row) {
      let data = {
        userId: localStorage.getItem("ID"),
        last_ip: sessionStorage.getItem("Ip"),
        id: row.id,
        title: row.title,
        name: row.name,
        examine: 1
      }
      this.$confirm('是否让该用户通过审核, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        queryIdEditExamine(data).then(res => {
          this.getqueryExamine()
          this.$message({
            type: 'success',
            message: res.msg
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消审核'
        });
      });
    },
    getqueryExamine(startSize = 1, pageSize = 10, type = 1, parameter) {
      //let qs = { startSize: startSize, pageSize: pageSize, type: type, userId: localStorage.getItem("ID"), ...parameter }
      queryExamine(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
        //console.log('请求', res)
      }).catch(err => {
        console.log('请求失败', err)
        this.browserTable = []
        this.total = 0
      })
    },
    //搜索,1:用户名查询，2手机号查询
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.type = 2
          this.listQuery.username = this.searchTable
          this.getqueryExamine();
          break;
        case '2':
          this.listQuery.type = 3
          this.listQuery.mobile = this.searchTable
          this.getqueryExamine();
          break;
      }
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryExamine();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryExamine();
    },
    Refresh() {
      this.listQuery = {
        startSize: 1,
        pageSize: 10,
        userId: localStorage.getItem("ID"),
        type: 1,
        username: undefined,
        mobile: undefined,
      }
      this.getqueryExamine()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>